package com.example.take_out_dish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.take_out_dish.config.JwtConfig;
import com.example.take_out_dish.entity.SetmealDish;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.mapper.DishMapper;
import com.example.take_out_dish.entity.Dish;
import com.example.take_out_dish.service.DishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.example.take_out_dish.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-07-01
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    HttpServletRequest request;

    @Resource
    JwtConfig jwtConfig;



//    @Override
//    public void saveDish(Dish dish) {
//        String token = "";
//        Cookie[] cookies = request.getCookies();
//        if(cookies!=null){
//            for(Cookie cookie : cookies){
//                if(cookie.getName().equals("token")){
//                    token=cookie.getValue();
//                }
//            }
//        }
//        log.info("token:{}",token);
//        if(!JwtUtils.validateToken(token,jwtConfig.getKey())){
//            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
//        }
//        Claims claims = JwtUtils.parseJwt(token,jwtConfig.getKey());
//        String name=claims.get("name",String.class);
//        dish.setUpdateUser(name);
//        dish.setCreateUser(name);
//
//        this.save(dish);
//    }

    @Override
    public void deleteById(String ids) {
        String[] idArray = ids.split(",");
        List<Long> idList = Arrays.stream(idArray)
                .map(Long::valueOf)
                .toList();

        // 执行删除操作
        idList.forEach(id -> {
            Dish dish = getById(id);
            if (dish != null) {
                dish.setIsDeleted(1);
                updateById(dish);
            }
        });

    }

//    @Override
//    public Dish updateDish(Dish dish) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("dish_id",dish.getId());
//        remove(queryWrapper);
//
//        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
//            setmealDish.setSetmealId(setmeal.getId());
//            setmealDish.setCreateTime(new Date());
//            setmealDish.setUpdateTime(new Date());
//            setmealDish.setCreateUser(setmeal.getCreateUser());
//            setmealDish.setUpdateUser(setmeal.getCreateUser());
////            setmealService.updateById(setmeal);
//            setmealDishService.save(setmealDish);
//        }
//
//        updateById(dish);
//        return dish;
//    }

    @Override
    public void updateStatus(Dish one) {
        String token = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    token = cookie.getValue();
                }
            }
        }
        log.info("token:{}", token);
        if (!JwtUtils.validateToken(token, jwtConfig.getKey())) {
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);

        one.setCreateUser(name);
        one.setUpdateUser(name);

        Date now = new Date();
        one.setCreateTime(now);
        one.setUpdateTime(now);
        this.updateById(one);
    }

    @Override
    public List<Dish> listDish(Dish dish) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getStatus,1);
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        return list(wrapper);
    }
}
